﻿using LD.Host;
using LD.Host.Mes;
using System;
using System.Text;
using 右下角弹窗.DiagShow;

namespace LD.MES
{
    public partial class MesHelperDptFrm : BaseFrm
    {
        public MesHelperDptFrm()
        {
            InitializeComponent();
            this.Shown += MesHelperSharpFrm_Shown;
        }

        private void MesHelperSharpFrm_Shown(object sender, EventArgs e)
        {
            base.Permission(UserType.管理员);

            if (this.IsDisposed)
                return;
            Init();
        }
        private void Init()
        {
            this.txt_machineCode.Text = (string)Cache.GetCache(CONST.DPT_MACHINE);
            this.txt_port.Text = (string)Cache.GetCache(CONST.DPT_MESPORT);
            this.txt_mesType.Text = (string)Cache.GetCache(CONST.DPT_TYPE);
            this.txt_mesUrl.Text = (string)Cache.GetCache(CONST.DPT_URL);
            this.sw_ismes.Checked = (bool)Cache.GetCache(CONST.DPT_ISMES);
            this.sw_saveCSV.Checked = (bool)Cache.GetCache(CONST.DPT_ISSAVECSV);
        }
        public void Save()
        {
            Cache.SetCache(CONST.DPT_MACHINE, this.txt_machineCode.Text);
            Cache.SetCache(CONST.DPT_TYPE, this.txt_mesType.Text);
            Cache.SetCache(CONST.DPT_MESPORT, this.txt_port.Text);
            Cache.SetCache(CONST.DPT_URL, this.txt_mesUrl.Text);
            Cache.SetCache(CONST.DPT_ISMES, this.sw_ismes.Checked);
            Cache.SetCache(CONST.DPT_ISSAVECSV, this.sw_saveCSV.Checked);
            Dialog dialog = new Dialog();
            dialog.Success("保存成功");
        }

        private void btn_upload_Click(object sender, EventArgs e)
        {
            string machine = (string)Cache.GetCache(CONST.DPT_MACHINE);

            StringBuilder sb = new StringBuilder();
            sb.Append($"{this.txt_port.Text},");
            sb.Append($"{machine},");
            sb.Append($"{this.txt_barcode.Text},");
            sb.Append($"{this.txt_mesType.Text},");
            sb.Append($"OK,");
            sb.Append($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");

            bool result = MesHelperDpt.Post(sb.ToString());
            if (result)
                this.txt_result.Text = "上传成功";
            else
                this.txt_result.Text = "上传失败";
        }
    }
}
